package com.kscrm.web.service.impl;

import java.util.List;

import com.kscrm.common.core.domain.entity.SysUser;
import com.kscrm.common.exception.ServiceException;
import com.kscrm.system.service.ISysUserService;
import com.kscrm.web.dto.AddExpertDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.kscrm.web.mapper.ExpertMapper;
import com.kscrm.web.domain.Expert;
import com.kscrm.web.service.IExpertService;

/**
 * 专家Service业务层处理
 * 
 * @author Dhq
 * @date 2025-05-09
 */
@Service
public class ExpertServiceImpl implements IExpertService 
{
    @Autowired
    private ExpertMapper expertMapper;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询专家
     * 
     * @param id 专家主键
     * @return 专家
     */
    @Override
    public Expert selectExpertById(Long id)
    {
        return expertMapper.selectExpertById(id);
    }

    /**
     * 查询专家列表
     * 
     * @param expert 专家
     * @return 专家
     */
    @Override
    public List<Expert> selectExpertList(Expert expert)
    {
        return expertMapper.selectExpertList(expert);
    }

    /**
     * 新增专家
     * 
     * @return 结果
     */
    @Override
    public int insertExpert(AddExpertDto dto)
    {
        String account = dto.getAccount();
        // 判断该账号是否已注册用户
        SysUser user = sysUserService.selectUserByUserName(account);
        if(user != null){
            throw new ServiceException("该账号已注册为用户，请更换账号");
        }
        // 插入用户表
        Long userId = sysUserService.registerExpertUser(account,dto.getName());
        return expertMapper.insertExpert(new Expert()
                .setUserId(userId)
                .setName(dto.getName())
                .setPhone(dto.getPhone()));
    }

    /**
     * 修改专家
     * 
     * @param expert 专家
     * @return 结果
     */
    @Override
    public int updateExpert(Expert expert)
    {
        return expertMapper.updateExpert(expert);
    }

    /**
     * 批量删除专家
     * 
     * @param ids 需要删除的专家主键
     * @return 结果
     */
    @Override
    public int deleteExpertByIds(Long[] ids)
    {
        return expertMapper.deleteExpertByIds(ids);
    }

    /**
     * 删除专家信息
     * 
     * @param id 专家主键
     * @return 结果
     */
    @Override
    public int deleteExpertById(Long id)
    {
        return expertMapper.deleteExpertById(id);
    }
}
